package com.aspirecn.user.controller;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aspirecn.user.common.RestApiConstants;
import com.aspirecn.user.domain.ResponseHead;
import com.aspirecn.user.domain.ResponseMsg;

@ControllerAdvice
public class MyControllerAdvice {
    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseMsg errorHandler(Exception ex) {
        ResponseMsg response = new ResponseMsg();
        ResponseHead rspHead = new ResponseHead();
        rspHead.setRespStatus("Exception");
        rspHead.setRespCode(RestApiConstants.RESPCODE_FAILED);
        rspHead.setRespDesc("系统异常，请联系管理员");
        response.setHead(rspHead);
        return response;
    }

    /**
     * 拦截捕捉运行时异常 RuntimeException.class
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseMsg myErrorHandler(RuntimeException ex) {
        ResponseMsg response = new ResponseMsg();
        ResponseHead rspHead = new ResponseHead();
        rspHead.setRespStatus("RuntimeException");
        rspHead.setRespCode(RestApiConstants.RESPCODE_FAILED);
        rspHead.setRespDesc("请求处理失败， 请联系管理员");
        response.setHead(rspHead);
        return response;
    }
}
